package br.com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import br.com.bean.CadHistoricoNotaFaltaBean;

public class CadHistoricoNotaFaltaDao {

	public void inserir (Connection conn, CadHistoricoNotaFaltaBean value) throws Exception{
		
		String sql = "INSERT INTO cad_historico_nota_falta (id_aluno, id_cad_historico_letivo, diciplinas, carga_horaria" +
				"nota_final, falta_final) VALUES (?, ?, ?, ?, ?, ?)";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1,value.getIdaluno().getId());
		ps.setInt(2, value.getIdcadhistletivo().getId());
		ps.setInt(3, value.getDiciplinas().getId());
		ps.setInt(4, value.getCargahr());
		ps.setDouble(5, value.getNota());
		ps.setInt(6, value.getFaltafinal());
		ps.execute();
		ps.close();
		
	}
	
	public void alterar (Connection conn, CadHistoricoNotaFaltaBean value) throws Exception{
		
		String sql = "UPDATE cad_historico_nota_falta SET id_aluno = ?, id_cad_historico_letivo = ?, diciplinas = ?, carga_horaria = ?" +
				"nota_final = ?, falta_final = ? WHERE  id = ?";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1,value.getIdaluno().getId());
		ps.setInt(2, value.getIdcadhistletivo().getId());
		ps.setInt(3, value.getDiciplinas().getId());
		ps.setInt(4, value.getCargahr());
		ps.setDouble(5, value.getNota());
		ps.setInt(6, value.getFaltafinal());
		ps.execute();
		ps.close();

	}
	
	public void excluir (Connection conn, CadHistoricoNotaFaltaBean value) throws Exception {

		String sql = "DELETE FROM cad_historico_nota_falta WHERE where id = ?";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, value.getId());
		ps.executeUpdate();
	}
	
	public ArrayList<CadHistoricoNotaFaltaBean> getLista(Connection conn, CadHistoricoNotaFaltaBean value) throws Exception{

		String sql = "SELECT id_aluno, id_cad_historico_letivo, diciplinas, carga_horaria" +
				"nota_final, falta_final FROM cad_historico_nota_falta where id = ?";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, value.getId());
		ResultSet rs = ps.executeQuery();
		ArrayList<CadHistoricoNotaFaltaBean> cadhistoricoNota = new ArrayList<CadHistoricoNotaFaltaBean>();
		while(rs.next()){
			CadHistoricoNotaFaltaBean a = new CadHistoricoNotaFaltaBean();
			a.getIdaluno().setId(rs.getInt("id_aluno"));
			a.getIdcadhistletivo().setId(rs.getInt("id_cad_historico_letivo"));
			a.getDiciplinas().setId(rs.getInt("diciplinas"));
			a.setCargahr(rs.getInt("carga_horaria"));
			a.setNota(rs.getDouble("nota_final"));
			a.setFaltafinal(rs.getInt("falta_final"));
			cadhistoricoNota.add(a);
		}
		return cadhistoricoNota;
	}
}
